package com.zretc.notice.service;


import com.zretc.notice.Dao.NoticeDao;
import com.zretc.notice.pojo.Notice;
import com.zretc.notice.utils.ContentAuditor;
import com.zretc.notice.utils.SensitiveWordManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.List;

/**
 * 功能：
 * 作者：111
 * 日期：2024/7/3 16:23
 */
@Service
@Transactional
public class NoticeService {

    @Autowired
    NoticeDao noticeDao;

    public Notice showNoticeDetail(Integer noticeID){
        return noticeDao.findNoticeByNoticeId(noticeID);
    }

    //展示库内可展示公告
    public List<Notice> showNotice() {
        return noticeDao.findAllByIsShow(1);
    }

    //新增
    public void addNotice(Notice notice){
        if(notice.getCreatedTime()==null)
            notice.setCreatedTime(new Timestamp(System.currentTimeMillis()));
        notice.setIsShow(1);//展示
        notice.setIsDelete(0);//未删除
        Notice save = noticeDao.save(notice);
    }


    //检验id是否存在
    public boolean existsById(Integer noticeId){
        return noticeDao.existsById(noticeId);
    }

    //单个删除
    public void delete(Integer notice_id){

        Notice notice = noticeDao.findNoticeByNoticeId(notice_id);
        notice.setIsDelete(1);
        notice.setIsShow(0);
        Notice save = noticeDao.save(notice);
    }

    //批量删除
    public void batchDelete(List<Notice> noticeList){
        for(Notice notice:noticeList){
            notice.setIsDelete(1);
            Notice save =noticeDao.save(notice);
        }
    }



    //自动审核{true 审核通过  false 审核不通过}
    public boolean checkNotice(Notice notice){
        SensitiveWordManager sensitiveWordManager = new SensitiveWordManager();
        ContentAuditor contentAuditor = new ContentAuditor(sensitiveWordManager);
        notice.setState(contentAuditor.containsSensitiveWords(notice.getNoticeContent()));

        return contentAuditor.containsSensitiveWords(notice.getNoticeContent());
    }
    //更改
    public void updateNotice(Notice notice){

        notice.setIsShow(1);//展示
        notice.setIsDelete(0);//未删除
        Notice save = noticeDao.save(notice);
    }
}